package org.flexlancer.datavisualization
{
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	
	import org.flexlancer.Logger.Logger;
	import org.flexlancer.components.Node;
	import org.flexlancer.groups.GroupItem;

	public class DVCanvas extends Canvas
	{
		public var obj:LinkMap = new LinkMap();
		public var myGraphic:Shape = new Shape();
		public var mouseMoveX:int;
		public var mouseMoveY:int;
		
		//
		[Bindable]
		public var mouseDownX:int;
		[Bindable]
		public var mouseDownY:int;
		//public var canGroupObject:UIComponent = new UIComponent();
		private var groupX:Number = 0;
		private var groupY:Number = 0;
		private var hitObjects:Array = new Array();
		[Bindable]
		public var SelectedGroup:GroupItem = new GroupItem();
		
		public function DVCanvas()
		{
			try{
				super();
				this.obj.x = this.obj.y = 0;
				this.obj.percentWidth = this.obj.percentHeight = 100;
				this.obj.addEventListener(MouseEvent.MOUSE_UP,obj_OnMouseUp);
				this.addChild(this.obj);
				this.addEventListener(MouseEvent.MOUSE_DOWN,this.Me_OnMouseDown);
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		private var _items:Array = new Array();
		
		private function CheckHitTest(grp:GroupItem):void{
			try{
				var boolHitTest:Boolean = false;
				
				for ( var i:Number = 0 ; i < this.obj.numChildren; i ++ ){
					if ( this.obj.getChildAt(i) is Node ){
						var btn:Node = this.obj.getChildAt(i) as Node;
						if ( btn.hitTestObject(grp) ){
							grp.LinkedItem.push(btn);
							boolHitTest = true;
						}
					}
				}
				
				if ( !boolHitTest ) {
					this.RemoveGroup(grp);
				}else{
					grp.addEventListener("MoveGroup",OnMoveGroup);
				}
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		private function OnMoveGroup(evt:Event):void{
			this.obj.DrawAllLinks();
		}
		public function RemoveGroup(val:GroupItem):void{
			try{
				var index:Number = this._items.indexOf(val);
				this._items.splice(index,1);
				this.removeChild(val);
				
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		public function CPChangeevent(val:uint):void{
			try {
				this.SelectedGroup.setStyle("backgroundColor",val);
			}catch(err:Error) {
				Logger.LogE(err);
			}
		}

		public function OnNameEntered(parmName:String):void{
			try{
				var gInsert:GroupItem = new GroupItem();
				var w:int = this.mouseMoveX - this.mouseDownX;
				var h:int = this.mouseMoveY - this.mouseDownY;
				
				gInsert.x = this.mouseDownX;
				gInsert.y = this.mouseDownY;
				gInsert.width = w;
				gInsert.height = h;
				
				this._items.push(gInsert);
				this.addChild(gInsert);
				this.callLater(CheckHitTest,[gInsert]);
				
				gInsert.addEventListener(MouseEvent.CLICK,this.OnGrpMouseClick);
				gInsert.toolTip = parmName;
				//setTimeout(this.SetName,1000,gInsert,parmName);
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		private function SetName(parmGroup:GroupItem, parmName:String):void{
			parmGroup.group_label.text = parmName;
		}
		public function OnMakeGroup():void{
				try{
					/* var getName:twInputName = PopUpManager.createPopUp(this, twInputName,true) as twInputName;
					getName.caller = this;
					PopUpManager.centerPopUp(getName); */
					var gInsert:GroupItem = new GroupItem();
				var w:int = this.mouseMoveX - this.mouseDownX;
				var h:int = this.mouseMoveY - this.mouseDownY;
				
				gInsert.x = this.mouseDownX;
				gInsert.y = this.mouseDownY;
				gInsert.width = w;
				gInsert.height = h;
				
				this._items.push(gInsert);
				this.addChild(gInsert);
				this.callLater(CheckHitTest,[gInsert]);
				
				gInsert.addEventListener(MouseEvent.CLICK,this.OnGrpMouseClick);
				//gInsert.toolTip = parmName;
				}catch(err:Error){
					Logger.LogE(err);
				}
			}
			public function OnGrpMouseClick(evt:MouseEvent):void {
				try {
					this.SelectedGroup.setStyle("borderStyle","none")
					
					this.SelectedGroup = GroupItem(evt.currentTarget);
					DataVisualization_01(this.parentApplication).cp.selectedColor = this.SelectedGroup.getStyle("backgroundColor");
					
					this.SelectedGroup.setStyle("borderStyle","solid");
					this.SelectedGroup.setStyle("borderColor",0x000000);
					
					
				}catch(err:Error) {
					Logger.LogE(err);
				}
			}
			public function OnGrpAlphaChange(val:Number):void {
				try {
					this.SelectedGroup.alpha = val;
					
				}catch(err:Error) {
					Logger.LogE(err);
				}
				
			}			
		public function obj_OnMouseUp(evt:MouseEvent):void{
			try{
				this.parentApplication.SetMouseUpButton(this.obj.mouseUpButton);
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		public function AddNodeByName(parmName:String,parm_x:Number=-999,parm_y:Number=-999):String{
			try{
				if((parm_x == -999)&&(parm_y ==-999)) {
					var item_position:Number = this.obj.buttonArray.length*10;
				
					return this.obj.AddItem(parmName,385+item_position,180+item_position);
				} 
				else 
				
					return this.obj.AddItem(parmName,parm_x,parm_y);
				
			}catch(err:Error){
				Logger.LogE(err);
			}
			return "";
		}
		
		public function AddNodeByInfo(label:String,
									  btn_x:Number=385,
									  btn_y:Number=180,
									  btn_width:Number=50,
									  btn_height:Number=50,
									  btn_Name:String=""):String 
		{
			try{
				return this.obj.AddItem(label,btn_x,btn_y,btn_width,btn_height,btn_Name);
			}catch(err:Error){
				Logger.LogE(err);
			}
			return "";
		}
		
		public function AddLinkByName(parmNode1Name:String, parmNode2Name:String,parmReason:String="",parmDesc:String="",parmType:String=""):void{
			try{
				var b1:Node = Node(this.obj.getChildByName(parmNode1Name));
				var b2:Node = Node(this.obj.getChildByName(parmNode2Name));
				
				this.obj.btn_01 = b1;
				this.obj.btn_02 = b2;
				
				this.obj.ADDLink(b1,b2,parmReason,parmDesc,parmType);
				
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		public function RemoveLink(parmNode1Name:String,parmNode2Name:String):void{
			try{
				this.obj.RemoveLink(parmNode1Name,parmNode2Name);
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		public function RemoveButton(parmNodeName:String):Boolean{
			try{
				return this.obj.RemoveItem(parmNodeName);
				
			}catch(err:Error){
				Logger.LogE(err);
			}
			return false;
		}
		
		public function SetPositionOfButton(parmName:String,parmX:Number, parmY:Number):void{
			try{
				var btn:Node = this.obj.getChildByName(parmName) as Node;
				btn.x = parmX;
				btn.y = parmY;
				this.obj.DrawAllLinks();
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		public function AddLinkToSelected():void{
			try{
				this.obj.ADDLink(this.obj.btn_01,this.obj.btn_02);
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		public function RemoveSelectedLink():void{
			try{
				this.obj.RemoveLink(this.obj.btn_01.name,this.obj.btn_02.name);
				
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		public function Me_OnMouseDown(evt:MouseEvent):void{
			try{
				for ( var j:Number = 0 ; j < this.obj.buttonArray.length ; j ++ )
				{
					var checkButton:Node = Node(this.obj.buttonArray[j]);
					checkButton.selected = false;
					
				} 	
				
				this.mouseDownX = this.mouseX;
				this.mouseDownY = this.mouseY;	
				this.addEventListener(MouseEvent.MOUSE_MOVE,Me_OnMouseMove);
				this.addEventListener(MouseEvent.MOUSE_UP,Me_OnMouseUp);
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		public function Me_OnMouseMove(evt:MouseEvent):void{
			try{
				
					
					this.myGraphic.graphics.clear();
					this.myGraphic.graphics.lineStyle( .5, 0x0099FF );
					this.myGraphic.graphics.beginFill( 0x0099FF, 0.2);
					this.mouseMoveX = this.mouseX;
					this.mouseMoveY = this.mouseY;;
					
					var w:int;
					w = this.mouseMoveX - this.mouseDownX;
					var h:int;
					h= this.mouseMoveY - this.mouseDownY;
					this.myGraphic.graphics.drawRect(this.mouseDownX,this.mouseDownY,w,h);
					this.rawChildren.addChild(this.myGraphic);
					
					///////////
				//var moveButton:Button = Button(this.buttonArray[0]);
				
				for ( var j:Number = 0 ; j < this.obj.buttonArray.length ; j ++ )
				{
					var checkButton:Node = Node(this.obj.buttonArray[j]);
					
				
					
					if(this.myGraphic.hitTestObject(checkButton)){
						checkButton.selected = true;
					
						//Alert.show("Ouch!!!");
					} else {
						checkButton.selected = false;
					}	
				} 				/////////////
			
			
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		public function Me_OnMouseUp(evt:MouseEvent):void{
			try{
				this.removeEventListener(MouseEvent.MOUSE_UP,this.Me_OnMouseUp);
				this.removeEventListener(MouseEvent.MOUSE_MOVE,this.Me_OnMouseMove);
				this.myGraphic.graphics.clear();
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		public function getXML():XML{
			try{
				var xmlObject:XML = new XML("<Contents />");
				xmlObject.appendChild(this.obj.getXML());
				xmlObject.appendChild(this.obj.getLinksXML());
				return xmlObject;
			}catch(err:Error){
				Logger.LogE(err);
			}
			return null;
		}
	}
}